//
//  SiterNetManager.m
//  sHome
//
//  Created by TracyHenry on 2022/8/15.
//  Copyright © 2022年 SiterWell. All rights reserved.
//

#import "SiterNetManager.h"
#import "SiterAPI.h"

@implementation SiterNetManager


//清除所有报警记录
+ (id)clearAllWarningsWithDevTid:(NSString *)devTid ctrlKey:(NSString *)ctrlKey handler:(void (^)(NSError *))handler {
    NSDictionary *params = @{@"devTid": devTid,
                             @"ctrlKey": ctrlKey,
                             @"type": @"WARNING"
                             };
    return [self DELETE:[NSString stringWithFormat:@"%@%@",[[Siter sharedInstance] getDomain],@"/api/v1/notification"] parameters:params completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(error);
    }];
}

//获取网关报警记录
+ (id)getWarningsWithDevTid:(NSString *)devTid andPage:(NSInteger)page handler:(void (^)(NSArray *, BOOL, NSError *))handler {
    NSDictionary *params = @{@"devTid": devTid,
                             @"page"  : @(page),
                             @"size"  : @20  };
    return [self GET:[NSString stringWithFormat:@"%@%@",[[Siter sharedInstance] getDomain],@"/api/v1/notification?type=WARNING"] parameters:params completionHandler:^(id responseObj, NSError *error) {
        BOOL last = [responseObj[@"last"] boolValue];
        NSArray *array = responseObj[@"content"];
        !handler ?: handler(array, last, error);
    }];
}

//获取当前游客账户邮箱
+ (id)getTourlistEmail:(void (^)(NSError *,NSString *email))handler {

    return [self GET:@"https://p4a4k81pc1.execute-api.us-west-2.amazonaws.com/default/getUserEmail" parameters:nil completionHandler:^(id responseObj, NSError *error) {

        !handler ?: handler(error,responseObj[@"email"]);
    }];
}

//设置当前游客账户邮箱
+ (id)setTourlistEmail:(NSString *)email withHandler:(void (^)(NSError *))handler {

    NSDictionary *params = @{@"email": email};
    
    return [self POST:@"https://7v09adtsuc.execute-api.us-west-2.amazonaws.com/default/setEmail" parameters:params completionHandler:^(id responseObj, NSError *error) {

        !handler ?: handler(error);
    }];
}


//图形校验
+ (id) checkImage:(NSString *)code withRandom:(NSString *)ramdomcode handle:(void(^)(id,NSError *)) handler{
 
    NSDictionary *param = @{@"rid": ramdomcode,
                            @"code": code };
    
    
    return [self POST:[NSString stringWithFormat:@"%@/images/checkCaptcha",[[Siter sharedInstance] getDomain]] parameters:param completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//注册获取验证码(邮箱或者手机)
+ (id) registGetVerifyCode:(NSString *)token withType:(BOOL)isPhone withAccount:(NSString *)account handle:(void(^)(id,NSError *)) handler{
 
    NSString * url = (isPhone?@"%@/sms/getVerifyCode?phoneNumber=%@&pid=%@&token=%@&type=register":@"%@/email/getVerifyCode?email=%@&pid=%@&token=%@&type=register");
    NSString *pid = [Siter sharedInstance].pid;
    
    return [self GETMULLAN:[NSString stringWithFormat:url,[[Siter sharedInstance] getDomain],account,pid,token] parameters:nil completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//注册账号(邮箱或者手机)
+ (id)regist:(NSString *)account withPassword:(NSString *)pasword with:(NSString *)code withType:(BOOL)isPhone handle:(void(^)(id,NSError *)) handler{
 
    NSString *url = [NSString stringWithFormat:@"%@/register?type=%@",[[Siter sharedInstance] getDomain],isPhone?@"phone":@"email_verify_code"];
    
    NSString *pid = [Siter sharedInstance].pid;
    
    NSDictionary *param = @{
                             @"pid" :pid,
                             (isPhone?@"phoneNumber":@"email"): account,
                             @"password": pasword,
                             @"code" : code
                             };
    
    return [self POST:url parameters:param completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}


//重置获取验证码(邮箱或者手机)
+ (id) resetGetVerifyCode:(NSString *)token withType:(BOOL)isPhone withAccount:(NSString *)account  handle:(void(^)(id,NSError *)) handler{
    NSString *pid = [Siter sharedInstance].pid;
    NSString * url = (isPhone?@"%@/sms/getVerifyCode?phoneNumber=%@&pid=%@&token=%@&type=resetPassword":@"%@/email/getVerifyCode?email=%@&pid=%@&token=%@&type=resetPassword");
    
    return [self GETMULLAN:[NSString stringWithFormat:url,[[Siter sharedInstance] getDomain],account,pid,token] parameters:nil completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//重置账号(邮箱或者手机)
+ (id)reset:(NSString *)account withPassword:(NSString *)pasword with:(NSString *)code withType:(BOOL)isPhone handle:(void(^)(id,NSError *)) handler{
 
    NSString *url = [NSString stringWithFormat:@"%@/resetPassword?type=%@",[[Siter sharedInstance] getDomain],isPhone?@"phone":@"email_verify_code"];

    NSString *pid = [Siter sharedInstance].pid;

    NSDictionary *param = @{
                             @"pid" :pid,
                             (isPhone?@"phoneNumber":@"email"): account,
                             @"password": pasword,
                             @"verifyCode" : code
                             };
    
    return [self POST:url parameters:param completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}


//获取网关列表
+ (id) getDeviceList:(NSDictionary *)params handle:(void(^)(id,NSError *)) handler{

    return [self GET:[NSString stringWithFormat:@"%@/userAws/deviceList",[[Siter sharedInstance] getDomain]] parameters:params completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}




//绑定网关
+ (id) bindDevice:(NSDictionary *)params handle:(void(^)(id,NSError *)) handler{

    return [self POST:@"https://s8ktr0jrhf.execute-api.us-west-2.amazonaws.com/default/bindDevice" parameters:params completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//绑定网关
+ (id) unbindDevice:(NSDictionary *)params handle:(void(^)(id,NSError *)) handler{

    return [self POST:@"https://n3lgfbyss4.execute-api.us-west-2.amazonaws.com/default/unbindDevice" parameters:params completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}


//获取账号资料
+ (id) getProfile:(void(^)(id,NSError *)) handler{

    return [self GET:[NSString stringWithFormat:@"%@/user/profile",[[Siter sharedInstance] getDomain]] parameters:nil completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//修改账号资料
+ (id) modifyProfile:(NSDictionary *)params handle:(void(^)(id,NSError *)) handler{

    return [self PUT:[NSString stringWithFormat:@"%@/user/profile",[[Siter sharedInstance] getDomain]] parameters:params completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//修改账号密码
+ (id) modifyPassword:(NSDictionary *)params handle:(void(^)(id,NSError *)) handler{

    return [self POST:[NSString stringWithFormat:@"%@/changePassword",[[Siter sharedInstance] getDomain]] parameters:params completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//绑定FCM推送接口
+ (id) bindPushTag:(NSString *)register_token handle:(void(^)(id,NSError *)) handler{
    NSDictionary *params = @{@"register_token":register_token};
    return [self POST:@"https://9b40antgr9.execute-api.us-west-2.amazonaws.com/default/bindPushTag" parameters:params completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}


//获取PINCODE (用于)
+ (id) getPinCode:(void(^)(id,NSError *)) handler{

    return [self GET:[NSString stringWithFormat:@"%@/getPINCode?ssid=siterWell",[[Siter sharedInstance] getDomain]] parameters:nil completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//解绑所有接口
+ (id) unbindAllPushAlias:(void(^)(id,NSError *)) handler{

    return [self DELETE:[NSString stringWithFormat:@"%@/user/unbindAllPushAlias",[[Siter sharedInstance] getDomain]] parameters:nil completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//解绑特定的接口
+ (id) unbindPushAlias:(NSDictionary *)params handle:(void(^)(id,NSError *)) handler{

    return [self POST:[NSString stringWithFormat:@"%@/user/unbindPushAlias",[[Siter sharedInstance] getDomain]] parameters:params completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//修改网关名称
+ (id) modifyDeviceName:(NSString *)deviceName withDevTid:(NSString *)devTid withCtrlKey:(NSString *)ctrlKey handle:(void(^)(id,NSError *)) handler{
    NSDictionary *dic = @{
                          @"deviceName" : deviceName,
                          @"desc" : @"1",
                          @"ctrlKey" : ctrlKey
                          };
    return [self PATCH:[NSString stringWithFormat:@"%@/device/%@",[[Siter sharedInstance] getDomain],devTid] parameters:dic completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//获取网关OTA信息
+ (id) getOTA:(NSArray *)params handle:(void(^)(id,NSError *)) handler{

    return [self POST:[NSString stringWithFormat:@"%@/external/device/fw/ota/check",[[Siter sharedInstance] getDomain]] parameters:params completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//解绑所有接口
+ (id) deleteDevice:(NSString *)devTid withBindKey:(NSString *)bindKey handle:(void(^)(id,NSError *)) handler{

    return [self DELETE:[NSString stringWithFormat:@"%@/device/%@",[[Siter sharedInstance] getDomain],devTid] parameters:@{@"bindKey":bindKey} completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

//获取新的网关设备
+ (id) getNewDeviceList:(NSString *)code handle:(void(^)(id,NSError *)) handler{

    return [self GET:[NSString stringWithFormat:@"%@/getNewDeviceList?pinCode=%@&ssid=siterWell",[[Siter sharedInstance] getDomain],code] parameters:nil completionHandler:^(id responseObj, NSError *error) {
        !handler ?: handler(responseObj, error);
    }];
}

@end
